package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.UserModel;
import com.zeng.jiudian.bean.UserModelExample;
import com.zeng.jiudian.bean.UserModelExample.Criteria;
import com.zeng.jiudian.dao.UserDao;
import com.zeng.jiudian.mapper.UserMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/16-15:37
 */
public class UserDaoImpl implements UserDao
{
    private SqlSession sqlSession;

    @Override
    public int addUser(UserModel userModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userModel.setCreateTime(dateFormat.format(new Date()));
        userModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            count = sqlSession.getMapper(UserMapper.class).insert(userModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public boolean findByUsername(String username)
    {
        boolean isHave = false;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            UserModelExample example = new UserModelExample();
            Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(username);
            List<UserModel> models = userMapper.selectByExample(example);
            if (models.size() > 0)
            {
                isHave = true;
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return isHave;
    }

    @Override
    public boolean findByUsernameAndPassword(UserModel userModel)
    {
        boolean isHave = false;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            UserModelExample example = new UserModelExample();
            Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(userModel.getUsername());
            criteria.andPasswordEqualTo(userModel.getPassword());
            List<UserModel> models = userMapper.selectByExample(example);
            if (models.size() > 0)
            {
                isHave = true;
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return isHave;
    }

    @Override
    public List<UserModel> findAll(UserModel userModel)
    {
        List<UserModel> list = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            list = userMapper.selectAll(userModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int deleteById(int id)
    {
        int count = 0;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            count = userMapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public UserModel findById(int id)
    {
        UserModel userModel = null;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            userModel = userMapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return userModel;
    }

    @Override
    public int edit(UserModel userModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userModel.setUpdateTime(dateFormat.format(new Date()));
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            UserModel model = findById(userModel.getId());

            if (!model.getUsername().equals(userModel.getUsername()))
            {
                if (findByUsername(userModel.getUsername()))
                {
                    count = -1;
                }
                else
                {
                    count = userMapper.updateByPrimaryKeySelective(userModel);
                }
            }
            else
            {
                count = userMapper.updateByPrimaryKeySelective(userModel);
            }

        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int getCount(UserModel userModel)
    {
        int count = 0;
        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            count = userMapper.selectCount(userModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }
}
